using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Web.Security;
using System.Data.Entity;
using DomainModel.Concrete;
using DomainModel.Entities;

namespace WebUI.Controllers
{
    public class VehicleBrandController : Controller
    {
        SqlVehicleBrandRepository _vehicleBrandRepository;
        #region PrivateFunctions
        private ActionResult HandleMyError(Exception ex, string actionName)
        {
            var modelErr = new HandleErrorInfo(ex, "VehicleBrandController", actionName);
            return View("Error", modelErr);
        }
        #endregion
        public VehicleBrandController()
        {
            _vehicleBrandRepository = new SqlVehicleBrandRepository();
        }
        #region PublicActions
        public ActionResult Index()
        {
            try
            {
                return View(_vehicleBrandRepository.GetAll());
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "Index Action");
            }

        }

        public ActionResult Details(int id)
        {
            try
            {
                return View(_vehicleBrandRepository.GetById(id));
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "Get Detail Action");
            }
        }

        public ActionResult Create()
        {
            try
            {
                VehicleBrand vehicleBrand = new VehicleBrand();
                return View(vehicleBrand);
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "Get Create Action");
            }

        }

        [HttpPost]
        public ActionResult Create(VehicleBrand vehiclebrand, FormCollection collection)
        {
            vehiclebrand.LastUpdate = DateTime.Now;
            vehiclebrand.UserName = Membership.GetUser().UserName;
            vehiclebrand.IsApproved = true;
            ViewBag.PicturePath = GlobalController.GetPicturePathForBrands();

            if (ModelState.IsValid)
            {
                try
                {
                    _vehicleBrandRepository.Insert(vehiclebrand);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    return HandleMyError(ex, "Post Create Action");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Edit(int id)
        {
            try
            {
                return View(_vehicleBrandRepository.GetById(id));
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "Get Edit Action");
            }
        }

        [HttpPost]
        public ActionResult Edit(VehicleBrand vehiclebrand, int id, FormCollection collection)
        {
            vehiclebrand.LastUpdate = DateTime.Now;
            if (ModelState.IsValid)
            {
                try
                {
                    _vehicleBrandRepository.Update(vehiclebrand);
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    return HandleMyError(ex, "Post Edit Action");
                }
            }
            else
            {
                return RedirectToAction("Index");
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                return View(_vehicleBrandRepository.GetById(id));
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "Get Delete Action");
            }

        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                _vehicleBrandRepository.Delete(_vehicleBrandRepository.GetById(id));
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return HandleMyError(ex, "Post Delete Action");
            }
        }
        #endregion
    }
}
